/**  
*  Doku2Buddy - A DokuWiki to BuddyPress Wiki-Component Converter
*  Copyright (C) 2010 Skye Book
*  
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*  
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*  
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.sbook.d2b.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.sbook.d2b.ConversionCatalyst;
import com.sbook.d2b.db.DBManager;

/**
 * @author Skye Book
 *
 */
public class StartupGUI extends JFrame{
	private static final long serialVersionUID = 1L;

	private JLabel dokuDir;
	private JLabel dokuURL;
	private JLabel dbName;
	private JLabel dbUser;
	private JLabel dbPass;
	private JLabel dbHost;
	private JLabel dbPort;

	private JTextField dokuField;
	private JTextField dokuURLField;
	private JTextField dbNameField; 
	private JTextField dbUserField; 
	private JPasswordField dbPassField;
	private JTextField dbHostField;
	private JTextField dbPortField;

	private JButton dokuBrowseButton;
	private JButton goButton;
	
	private JButton getGroups;
	private JComboBox groupSelector;
	
	private JTabbedPane tabbedPane;
	
	private JPanel connectionInfoPane;
	private JPanel bpGroupsPane;
	
	private JProgressBar progressBar;
	
	GridBagConstraints constraints = new GridBagConstraints();

	/**
	 * 
	 */
	public StartupGUI() {
		super("Doku2Buddy Converter");
		
		this.setLayout(new GridBagLayout());
		this.setSize(650, 300);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);

		
		constraints.insets=new Insets(0,5,0,5);
		
		createConnectionInfoPane();
		createGroupsPane();
		
		tabbedPane = new JTabbedPane();
		tabbedPane.add("Connections", connectionInfoPane);
		tabbedPane.add("BP Groups", bpGroupsPane);
		this.add(tabbedPane);
	}
	
	private void createGroupsPane(){
		bpGroupsPane = new JPanel();
		bpGroupsPane.setLayout(new GridBagLayout());
		
		constraints.gridx=1;
		constraints.gridy=1;
		JLabel groupLabel = new JLabel("Default Group");
		bpGroupsPane.add(groupLabel, constraints);
		
		constraints.gridx=2;
		groupSelector = new JComboBox(new String[]{"Groups..."});
		groupSelector.setEditable(false);
		groupSelector.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(groupSelector.getSelectedItem()!=null){
					System.out.println("selection: " + groupSelector.getSelectedItem().toString());
				}
			}
		});
		bpGroupsPane.add(groupSelector, constraints);
		
		constraints.gridx=1;
		constraints.gridy=2;
		goButton = new JButton("Go!");
		goButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				doConversion();
			}
		});
		bpGroupsPane.add(goButton, constraints);
	}
	
	private void createConnectionInfoPane(){
		connectionInfoPane = new JPanel();
		connectionInfoPane.setLayout(new GridBagLayout());
		
		setupRow1();
		
		setupRow2();

		setupRow3();

		setupRow4();

		setupRow5();

		setupRow6();

		setupRow7();
		
		setupRow8();
	}
	
	private void setupRow8(){
		// Row 7 - Progress bar
		constraints.gridy=7;
		progressBar = new JProgressBar();
		constraints.gridx = 1;
		connectionInfoPane.add(progressBar, constraints);
	}

	private void setupRow7() {
		// Row 6 - Go & Cancel
		constraints.gridy=6;
		getGroups = new JButton("Retrieve Groups");
		getGroups.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				String pass = "";
				
				for(char aChar : dbPassField.getPassword()){
					pass+=aChar;
				}
				DBManager dbMan = new DBManager(dbUserField.getText(), pass, dbNameField.getText(), dbHostField.getText(), Integer.parseInt(dbPortField.getText()), "http://localhost/wordpress");
				ArrayList<String> groups = dbMan.getAllGroups();
				// get the groups
				groupSelector.removeAllItems();
				
				for(String groupName : groups){
					groupSelector.addItem(groupName);
				}
				
				tabbedPane.setSelectedComponent(bpGroupsPane);
			}
		});
		constraints.gridx=0;
		connectionInfoPane.add(getGroups, constraints);
	}

	private void setupRow6() {
		// Row 5 - Database host & port
		constraints.gridy=5;
		dbHost = new JLabel("Database Host");
		constraints.gridx=0;
		connectionInfoPane.add(dbHost, constraints);
		dbHostField = new JTextField();
		dbHostField.setText("localhost");
		constraints.gridx=1;
		connectionInfoPane.add(dbHostField, constraints);
		dbPort = new JLabel("Database Port");
		constraints.gridx=2;
		connectionInfoPane.add(dbPort, constraints);
		dbPortField = new JTextField();
		dbPortField.setText("3306");
		//constraints.ipadx=0;
		constraints.gridx=3;
		connectionInfoPane.add(dbPortField, constraints);
		//constraints.ipadx=0;
	}

	private void setupRow5() {
		// Row 4 - Database password
		constraints.gridy=4;
		dbPass = new JLabel("Database Password");
		constraints.gridx=0;
		connectionInfoPane.add(dbPass, constraints);
		dbPassField = new JPasswordField();
		constraints.gridx=1;
		connectionInfoPane.add(dbPassField, constraints);
	}

	private void setupRow4() {
		// Row 3 - Database username
		constraints.gridy=3;
		dbUser = new JLabel("Database Username");
		constraints.gridx=0;
		connectionInfoPane.add(dbUser, constraints);
		dbUserField = new JTextField();
		constraints.gridx=1;
		connectionInfoPane.add(dbUserField, constraints);
	}

	private void setupRow3() {
		// Row 2 - Wordpress database name
		constraints.gridy=2;
		dbName = new JLabel("WP Database Name");
		constraints.gridx=0;
		connectionInfoPane.add(dbName, constraints);
		dbNameField = new JTextField();
		constraints.gridx=1;
		connectionInfoPane.add(dbNameField, constraints);
	}
	
	private void setupRow2(){
		// Row 1 - DokuWiki URL
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridy=1;
		dokuURL = new JLabel("Doku URL");
		constraints.gridx=0;
		connectionInfoPane.add(dokuURL, constraints);
		dokuURLField = new JTextField();
		constraints.ipadx=150;
		constraints.gridx=1;
		connectionInfoPane.add(dokuURLField, constraints);
	}

	private void setupRow1(){
		// Row 1 - DokuWiki installation
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridy=0;
		dokuDir = new JLabel("Doku Install Directory");
		constraints.gridx=0;
		connectionInfoPane.add(dokuDir, constraints);
		dokuField = new JTextField();
		constraints.ipadx=150;
		constraints.gridx=1;
		connectionInfoPane.add(dokuField, constraints);
		dokuBrowseButton = new JButton("Browse..");
		dokuBrowseButton.setToolTipText("Locate the path of your DokuWiki Installation");

		dokuBrowseButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int value = fileChooser.showOpenDialog(new JFrame());
				while(true){
					if(value == JFileChooser.APPROVE_OPTION){
						dokuField.setText(fileChooser.getSelectedFile().toString());
						break;
					}
					if(value == JFileChooser.CANCEL_OPTION){
						break;
					}
				}
			}
		});
		constraints.ipadx=0;
		constraints.gridx=2;
		connectionInfoPane.add(dokuBrowseButton, constraints);
	}
	
	private void doConversion(){
		ConversionCatalyst converter = new ConversionCatalyst(new File(dokuField.getText()), dokuURLField.getText(), dbNameField.getText(), dbUserField.getText(), dbPassField.getPassword(), dbHostField.getText(), Integer.parseInt(dbPortField.getText()), groupSelector.getSelectedItem().toString());
	}
}